from PySide6 import QtCore, QtWidgets, QtGui
from time import sleep
import atomacos
import sys



class View1(QtWidgets.QWidget):
    def __init__(self):
        super().__init__()
        self.text = QtWidgets.QLabel("Hello", alignment=QtCore.Qt.AlignCenter)
        self.button = QtWidgets.QPushButton("click")
        self.edit = QtWidgets.QTextEdit("")
        self.layout = QtWidgets.QVBoxLayout(self)
        self.layout.addWidget(self.text)
        self.layout.addWidget(self.edit)
        self.layout.addWidget(self.button)

        self.button.clicked.connect(self.meth1)

    @QtCore.Slot()
    def meth1(self):
        if True:
            for i in self.edit.toPlainText():
                callAppl1("com.apple.Safari", i)
                sleep(0.2)
        else:
            callAppl1("com.apple.Safari", self.edit.toPlainText())



def callAppl1(bundleID, text):
    atomacos.launchAppByBundleId(bundleID)
    while len(atomacos.getAppRefByBundleId(bundleID).windows()) == 0:
        sleep(1)
    wind = atomacos.getAppRefByBundleId(bundleID).windows()[0]
    obj_a = wind.findFirstR(AXRole='AXWebArea')
    obj_a.sendKeys(text)



if __name__ == '__main__':
    app = QtWidgets.QApplication([])
    view = View1()
    view.resize(800, 600)
    view.show()

    sys.exit(app.exec())
